var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
var app = getApp();

Page({
  data: {
    userInfo: {
      nickName: '点击登录',
      avatarUrl: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png'
    },
    order: {
      unconfirm: 0, //待确认
      unpaid: 0, //待付款
      unship: 0, //待发货
      recv: 0, //已收货
      deliver: 0, //已发货
    }
  },
  //获取提醒消息
  getNews:function(){
    util.request(api.loadNews).then(res => {
      this.setData({
        newsCount: res.data
      })
    })
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log('分享后的id：' + options.upperId)
    wx.setStorageSync('upperId', options.upperId)
  },
  onReady: function() {

  },
  onShow: function() {
    console.log('index onshow')
    console.log(app.globalData.hasLogin)
    this.getNews()
    //获取用户的登录信息
    if (app.globalData.hasLogin) {
      let userInfo = wx.getStorageSync('userInfo');
      console.log('进入信息盲区')
      console.log(userInfo)
      if (userInfo.avatarUrl== ''){
        console.log('没有头像')
        userInfo.avatarUrl = this.data.userInfo.avatarUrl
      }
      this.setData({
        userInfo: userInfo,
      });

      let that = this;
      util.request(api.UserIndex, {
        status: 'my'
      }).then(function(res) {
        if (res.errno === 0) {
          that.setData({
            order: res.data.order
          });
        }
      });
    }

  },
  //分享好友
  onShareAppMessage: function (res) {
    let that = this;
    return {
      title: '测试分享',
      // desc: that.data.goods.brief,
      path: '/pages/ucenter/index/index?upperId=' + wx.getStorageSync('userInfo').id,
      imageUrl: '/static/images/wechat.png'
    }
  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭
  },
  //跳转到个人资料
  toImformation: function(e) {
    wx.navigateTo({
      url: '/pages/ucenter/information/information',
    })
  },
  //跳转到消息
  toNews:function(){
    wx.navigateTo({
      url: '/pages/ucenter/news/news',
    })
  },
  goLogin() {
    if (!app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },
  goOrder() {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/order/order"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },
  goOrderIndex(e) {
    let tab = e.currentTarget.dataset.index
    let route = e.currentTarget.dataset.route
    try {
      wx.setStorageSync('tab', tab);
    } catch (e) {

    }
    wx.navigateTo({
      url: route,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  goCoupon() {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/coupon/coupon"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  // 团购
  goGroupon() {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/groupon/myTeam/myTeam"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  // 收藏
  goCollect() {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/collect/collect"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  // 跳转意见反馈
  goFeedback(e) {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/feedback/feedback"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  // 跳转到足迹
  goFootprint() {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/footprint/footprint"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  // 跳转到地址
  goAddress() {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/address/address"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  // 模板
  template(){
    wx.navigateTo({
      url: "/pages/test/test"
    });
  },
  //支付
  pay:function(e){
    console.log('支付')
    const orderId = 107;
    util.request(api.OrderPrepay, {
     orderId: orderId
    }, 'POST').then(function(res) {
     if (res.errno === 0) {
       console.log('进来啦')
      const payParam = res.data;
      console.log("支付过程开始");
      wx.requestPayment({
       'checkedGoodsList': payParam.timeStamp,
       'nonceStr': payParam.nonceStr,
       'package': payParam.packageValue,
       'signType': payParam.signType,
       'paySign': payParam.paySign,
       'success': function(res) {
        console.log("支付过程成功");
        wx.redirectTo({
         url: '/pages/payResult/payResult?status=1&orderId=' + orderId
        });
       },
       'fail': function(res) {
        console.log("支付过程失败");
        wx.redirectTo({
         url: '/pages/payResult/payResult?status=0&orderId=' + orderId
        });
       },
       'complete': function(res) {
        console.log("支付过程结束")
       }
      });
     } else {
       console.log('出去啦')
      wx.redirectTo({
       url: '/pages/payResult/payResult?status=0&orderId=' + orderId
      });
     }
    });
  },
  // 绑定手机
  bindPhoneNumber: function(e) {
    if (e.detail.errMsg !== "getPhoneNumber:ok") {
      // 拒绝授权
      return;
    }

    if (!app.globalData.hasLogin) {
      wx.showToast({
        title: '绑定失败：请先登录',
        icon: 'none',
        duration: 2000
      });
      return;
    }

    util.request(api.AuthBindPhone, {
      iv: e.detail.iv,
      encryptedData: e.detail.encryptedData
    }, 'POST').then(function(res) {
      if (res.errno === 0) {
        wx.showToast({
          title: '绑定手机号码成功',
          icon: 'success',
          duration: 2000
        });
      }
    });
  },
  goAfterSale: function() {
    wx.showToast({
      title: '目前不支持',
      icon: 'none',
      duration: 2000
    });
  },
  aboutUs: function() {
    wx.navigateTo({
      url: '/pages/about/about'
    });
  },
  exitLogin: function() {
    console.log("退出")
    wx.showModal({
      title: '',
      confirmColor: '#b4282d',
      content: '退出登录？',
      success: function(res) {
        if (res.confirm) {
          wx.removeStorageSync('token');
          wx.removeStorageSync('userInfo');
          wx.navigateTo({
            url: '/pages/auth/login/login'
          });
        }
      }
    })

  }
})